package com.kust.smarthome.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.smarthome.web.model.DeviceState;
import com.kust.smarthome.web.model.Television;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.IDeviceStateService;
import com.kust.smarthome.web.service.ITelevisionService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.DeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iAuzre
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/television")
@Api(value = "电视相关操作", tags = {"device-tag"})
public class  TelevisionController {

    @Resource
    private ITelevisionService televisionService;

    @Resource
    private IDeviceStateService deviceStateService;

    @Resource
    private IUserService userService;

    /**
     * 通过用户信息获取对应设备列表
     * @param principal Security框架中获取登录用户的详细信息
     * @return 对应用户的电视设备表
     */
    @GetMapping("")
    @ApiOperation(value = "获取电视信息", notes = "通过用户信息获取电视信息")
    public List<Television> list(Principal principal) {
        User u = userService.getByUsername(principal.getName());
        return televisionService.getByUserId(u.getId());
    }

    @GetMapping("/getById")
    @ApiOperation(value = "getById", notes = "通过id获取对应设备")
    public Television getById(Integer id) {
        return televisionService.getById(id);
    }

    /**
     * 客户端请求一次开关电视,传给后端id,若该id对应的灯的状态为1(开)则将状态变为0,反之变为1
     * 同时,在状态表中插入对应状态
     * @param id curtain.getId()
     * @return 返回所有的电视属性,前端js代码成功收到响应之后将vue中的数据重新绑定即可
     */
    @GetMapping("/update")
    @Transactional
    @ApiOperation(value = "开关电视", notes = "用户通过电视id开关电视操作")
    public List<Television> updateById(Principal principal, Integer id) {
        Television television = televisionService.getById(id);

        televisionService.updateById(television);
        DeviceState deviceState = new DeviceState()
                .setDeviceId(id)
                .setTypeId(5)
                .setIsUpdate(0);
        //更改状态通知
        QueryWrapper<DeviceState> queryD = new QueryWrapper<>();
        queryD.eq("device_id",id);
        queryD.eq("type_id",5);
        deviceStateService.update(deviceState,queryD);

        User user = userService.getByUsername(principal.getName());

        return televisionService.getByUserId(user.getId());
    }

    /**
     * 网页端用户添加电视,方法中初始化一个电视对象并插入到数据表中
     * 若对电视数量有限制,前端可以添加一个判定条件,是否执行异步请求,同时后端也会做一次判定
     * 最大数量先定为3
     * @return 返回插入之后的电视列表
     */
    @GetMapping("/add")
    @Transactional
    @ApiOperation(value = "添加电视", notes = "用户提供电视id进行添加电视操作")
    public List<Television> addCurtain(Principal principal, DeviceVo deviceVo){

        User u = userService.getByUsername(principal.getName());

        List<Television> list = televisionService.getByUserId(u.getId());

        LocalDateTime now = LocalDateTime.now();

        if (list.size() < 3) {
            Television television = new Television()
                    .setId(deviceVo.getId())
                    .setUrl("")
                    .setContent("这是一台电视")
                    .setState(0)
                    .setCreated(now)
                    .setUserId(u.getId())
                    .setRoomId(deviceVo.getRoomId());
            televisionService.insert(television);
            list.add(television);
        }

        DeviceState deviceState = new DeviceState()
                .setDeviceId(deviceVo.getId())
                .setTypeId(5)
                .setIsUpdate(0);
        //插入状态变更通知
        deviceStateService.insert(deviceState);

        return list;
    }

    /**
     * 删除一个电视对象操作
     * 同时要删除其所对应的状态表
     * @param id 由客户端传入一个id
     * @return 当后端处理完删除操作之后返回当前数据库中的电视对象列表
     */
    @GetMapping("/deleteById")
    @Transactional
    @ApiOperation(value = "删除电视", notes = "用户通过电视id删除电视")
    public List<Television> deleteById(Principal principal, Integer id) {
        Television television = televisionService.getById(id);
        television.setUserId(-1);
        televisionService.updateById(television);
        QueryWrapper<DeviceState> query = new QueryWrapper<>();
        query.eq("device_id",television.getId());
        query.eq("type_id",5);
        deviceStateService.remove(query);

        User u = userService.getByUsername(principal.getName());
        return televisionService.getByUserId(u.getId());
    }

}
